package com.tjshop.android.fragment;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.tjshop.android.AllOrderActivity;
import com.tjshop.android.MyAddressActivity;
import com.tjshop.android.MyCollectionActivity;
import com.tjshop.android.MyCommentActivity;
import com.tjshop.android.MyMessageActivity;
import com.tjshop.android.NoGoodsActivity;
import com.tjshop.android.R;
import com.tjshop.android.AboutApp;
import com.tjshop.android.LoginActivity;
import com.tjshop.android.MineActivity;
import com.tjshop.android.MineCoupon;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.mcv.GlobeVars;
import com.tjshop.android.model.tj.json.Category;
import com.tjshop.android.model.tj.json.UserInfo;
import com.tjshop.android.view.RefreshableView;
import com.tjshop.android.view.RefreshableView.RefreshListener;

public class FragmentMine extends Fragment implements OnClickListener {

	private View MineView = null;
	private RequestQueue mQueue;

	void initEntryItem(View entryContainer, String text, int iconId) {
		TextView leftText = (TextView) entryContainer
				.findViewById(R.id.entry_item_left_icon);
		leftText.setText(text);
		leftText.setCompoundDrawablesWithIntrinsicBounds(iconId, 0, 0, 0);
	}

	void initEntryItemRightIcon(View entryContainer, int iconId) {
		ImageView imageView = (ImageView) entryContainer
				.findViewById(R.id.entry_item_right_icon);
		imageView.setImageResource(iconId);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mQueue = Volley.newRequestQueue(getActivity());

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v =  LayoutInflater.from(getActivity()).inflate(
				R.layout.activity_main_fragment_mine, null);
		((RefreshableView) v.findViewById(R.id.container)).setRefreshListener(new RefreshListener() {
			
			@Override
			public void onRefresh(RefreshableView view) {
				
			}
		});

		// login
		v.findViewById(R.id.activity_main_fragment_mine_login)
				.setOnClickListener(this);
		v.findViewById(R.id.rl_my_address).setOnClickListener(this);
		v.findViewById(R.id.rl_my_collection).setOnClickListener(this);
		v.findViewById(R.id.rl_my_comment).setOnClickListener(this);
		v.findViewById(R.id.rl_my_message).setOnClickListener(this);
		v.findViewById(R.id.rl_nogoods).setOnClickListener(this);
		v.findViewById(R.id.tv_all_order).setOnClickListener(this);
//		v.findViewById(R.id.login_user_area).setOnClickListener(this);

		// View myCoupon =
		// v.findViewById(R.id.activity_main_fragment_mine_my_coupon);
		// initEntryItem(myCoupon, "我的简单券", R.drawable.entry_mine_my_coupon);
		// myCoupon.setOnClickListener(this);
		// View upgradeVip =
		// v.findViewById(R.id.activity_main_fragment_mine_upgrade_vip);
		// initEntryItem(upgradeVip, "升级VIP",
		// R.drawable.entry_mine_upgrade_vip);
		// upgradeVip.setOnClickListener(this);
		//
		// View myEvent =
		// v.findViewById(R.id.activity_main_fragment_mine_my_event);
		// initEntryItem(myEvent, "我的活动", R.drawable.entry_mine_event);
		// myEvent.setOnClickListener(this);
		//
		// View about = v.findViewById(R.id.activity_main_fragment_mine_about);
		// initEntryItem(about, "关于", R.drawable.coupon_detail_saler);
		// about.setOnClickListener(this);
		//
		// View clearCache =
		// v.findViewById(R.id.activity_main_fragment_mine_clear_cache);
		// initEntryItem(clearCache, "清除缓存", R.drawable.entry_item_clear_cache);
		// initEntryItemRightIcon(clearCache,
		// R.drawable.entry_item_clear_cache_right);
		// if( GlobeVars.UserInfos != null){
		// v.findViewById(R.id.activity_main_fragment_mine_login).setVisibility(View.GONE);
		// v.findViewById(R.id.login_user_area).setVisibility(View.VISIBLE);
		// }else{
		// v.findViewById(R.id.activity_main_fragment_mine_login).setVisibility(View.VISIBLE);
		// v.findViewById(R.id.login_user_area).setVisibility(View.GONE);
		// }

		MineView = v;
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onPause() {
		super.onPause();
		boolean userVisibleHint = getUserVisibleHint();
		Log.w(FragmentMine.class.getSimpleName(), "onPause - userVisibleHint: "
				+ userVisibleHint);
		if (MineView != null) {
//			if (GlobeVars.isLogin) {
//				MineView.findViewById(R.id.activity_main_fragment_mine_login)
//						.setVisibility(View.GONE);
//				MineView.findViewById(R.id.login_user_area).setVisibility(
//						View.VISIBLE);
//				((TextView) (MineView.findViewById(R.id.login_user_area)
//						.findViewById(R.id.username)))
//						.setText("欢迎您,"+GlobeVars.userId);
//			} else {
//				MineView.findViewById(R.id.activity_main_fragment_mine_login)
//						.setVisibility(View.VISIBLE);
//				MineView.findViewById(R.id.login_user_area).setVisibility(
//						View.GONE);
//			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		if (MineView != null) {
			if (GlobeVars.isLogin) {
				MineView.findViewById(R.id.unlogined)
						.setVisibility(View.GONE);
				MineView.findViewById(R.id.logined)
				.setVisibility(View.VISIBLE);
				MineView.findViewById(R.id.logined).setVisibility(
						View.VISIBLE);
				((TextView) (MineView.findViewById(R.id.username)))
						.setText(GlobeVars.userId);
//				((TextView) (MineView.findViewById(R.id.username)))
//				.setText(GlobeVars.userId);
				getPageInfo();
			} else {
				MineView.findViewById(R.id.unlogined)
						.setVisibility(View.VISIBLE);
				MineView.findViewById(R.id.logined)
				.setVisibility(View.GONE);
			}
		}
		boolean userVisibleHint = getUserVisibleHint();
		Log.w(FragmentMine.class.getSimpleName(),
				"onResume - userVisibleHint: " + userVisibleHint);
	}
	UserInfo userInfo = new UserInfo();
	public void getPageInfo(){
		if (GlobeVars.isLogin) {
			StringRequest userinfo = new StringRequest(Method.GET,
					UrlConfigs.URL_GET_USER_INFO+"&t="+GlobeVars.token,
					new Response.Listener<String>() {
						@Override
						public void onResponse(String response) {
							JSONArray jsonU;
							try {
								JSONObject obj = new JSONObject(response);
								if (obj.getInt("r") == 0) {
									userInfo.parse(obj);
								} 
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}, new Response.ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError error) {
						}

					});
			StringRequest orderInfo = new StringRequest(Method.GET,
					UrlConfigs.URL_GET_ORDERS,
					new Response.Listener<String>() {
						@Override
						public void onResponse(String response) {
//							JSONArray jsonU;
//							try {
//								JSONObject obj = new JSONObject(response);
//								if (obj.getInt("r") == 0) {
//									jsonU = obj.getJSONArray("s");
//									for (int i = 0; i < jsonU.length(); i++) {
//										Category c = new Category();
//										c.parse(jsonU.getJSONObject(i));
//										categoryList.add(c);
//									}
//									Message message = new Message();
//									message.what = 2;
//								} else {
//									Message message = new Message();
//									message.what = 1;
//								}
//
//							} catch (JSONException e) {
//								e.printStackTrace();
//							}

						}
					}, new Response.ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError error) {
						}

					});

			mQueue.add(userinfo);
			mQueue.add(orderInfo);
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_main_fragment_mine_login:
			startActivity(new Intent(getActivity(), LoginActivity.class));
			break;
		case R.id.rl_my_address://我的地址
			startActivity(new Intent(getActivity(), MyAddressActivity.class));
			break;
		case R.id.rl_my_collection://我的收藏
			startActivity(new Intent(getActivity(), MyCollectionActivity.class));
			break;
		case R.id.rl_my_comment://我的评论
			startActivity(new Intent(getActivity(), MyCommentActivity.class));
			break;
		case R.id.rl_my_message://我的留言
			startActivity(new Intent(getActivity(), MyMessageActivity.class));
			break;
		case R.id.rl_nogoods://缺货登记
			startActivity(new Intent(getActivity(), NoGoodsActivity.class));
			break;
		case R.id.tv_all_order://所有订单
			startActivity(new Intent(getActivity(), AllOrderActivity.class));
			break;
		default:
			break;
		}

	}
}
